﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Drawing;
using System.Collections.Generic;
using ZX.WebApp;
using ZX.BLL.Extend;
using ZX.SYS.Bussiness;
using ZX.EHR.Library;


namespace ZX.WebApp.Crm.BasePages
{
    public partial class StartWorkflow : ZX.Web.UI.Controls.BasePage
	{
        string UserId = "";
        //string WorkflowId = "";
        //string WorktaskId = "";
        string WorkflowInsId = "";
        string WorktaskInsId = "";
        string title = "";//标题
        public string formTitle = "申请缴款单";
        ArrayList UserCtrlList = new ArrayList();//控件列表
        ZX.WebApp.CrmWorkFlowService.WorkFlowServiceClient workflow = new ZX.WebApp.CrmWorkFlowService.WorkFlowServiceClient();



        /// <summary>
        /// 表名
        /// </summary>
        public string TableName
        {
            get;
            set;
        }
        /// <summary>
        /// id
        /// </summary>
        public string KeyValue
        {
            get;
            set;
        }
        /// <summary>
        /// 流程ID
        /// </summary>
        public string WorkFlowId
        {
            get;
            set;
        }
        /// <summary>
        /// 流程务任ID
        /// </summary>
        public string WorkTaskId
        {
            get;
            set;
        }
        /// <summary>
        /// 表单类型
        /// </summary>
        public string FormType
        {
            get;
            set;
        }
        /// <summary>
        /// 页面标题
        /// </summary>
        public string PageTitle
        {
            get
            {
                return  Request["title"].ToString();
            }
        }

        public string IsIE
        {
            get
            {
                string isIE = "1";
                bool ise = UserPoint();
                isIE = (UserPoint()) ? "0" : "1";
                return isIE;
            }
        }
        

        protected void Page_Load(object sender, EventArgs e)
        {
            UserId = this.Page.User.Identity.Name;
            try
            {
                if (this.Session["User"] == null)
                {
                    Response.Redirect("../../Login.aspx");
                }
                if (!IsPostBack)
                {
                    //string tableName = this.GetStringFromQueryString("entity", "");
                    //string keyValue = this.GetStringFromQueryString("keyValue", "");
                    //if (tableName != "" && keyValue != "")
                    //{
                    //    TableName = tableName;
                    //    KeyValue = keyValue;
                    //    if (Start(tableName, keyValue))
                    //    {
                    //        Response.Write("更新成功");
                    //        workflow.Start(UserId, WorkFlowId, WorkTaskId, WorkflowInsId, WorktaskInsId, workflow.GetWorkFlowNO(), "提交", "3", "缴款单", false);
                    //        Response.Redirect("tooltip.aspx?worktaskInstanceId=" + WorktaskInsId);
                    //    }
                    //    Response.Write("0");
                    //    return;
                    //}
                    // BaseUserPage.LoadWaitPage();
                    Session["workinsid"] = Guid.NewGuid().ToString();
                    Session["worktasid"] = Request["WorktaskId"].ToString();
                    InitData();
                }


            }
            catch (Exception ex)
            {
                //LogData.WriteLog("平台打开启动界面错误:" + ex, lbWorkflowInsId.Text);

            }
            finally
            {
                if (!IsPostBack) { }
                //BaseUserPage.FreeWaitPage();
            }

        }
        private bool Start(string TableName, object KeyValue)
        {
            //ZX.SYS.Bussiness.WorkFlowStartInfo wfsi = new ZX.SYS.Bussiness.WorkFlowStartInfo();
            //return wfsi.Start(TableName, KeyValue);]
            return false;
        }
        public string URL
        {
            get;
            set;
        }
        private void InitData()
        {
            if (Request["WorkflowId"] != null && Request["WorktaskId"] != null)
            {
                WorkFlowId = Request["WorkflowId"].ToString();
                WorkTaskId = Request["WorktaskId"].ToString();
                string WorkFlowCaption = "";
                title = Request["title"].ToString();
                DataTable cdt = workflow.GetWorkTaskFormsView(WorkTaskId);
                WorkFlowCaption = Server.UrlEncode(workflow.GetWorkFlowCaption(WorkFlowId));
                if (cdt != null && cdt.Rows.Count > 0)
                {
                    FormType = cdt.Rows[0]["FormType"].ToString();

                    switch (FormType)
                    {
                        case "Html":
                            DataTable dt = workflow.GetTableEntity(cdt.Rows[0]["FilePath"].ToString());
                            string entityName = dt.Rows[0]["MasterTableName"].ToString();
                            string formid = dt.Rows[0]["FormID"].ToString();
                            TableName = entityName;
                            URL = "../../Extend/AutoLoadForm.aspx?entity=" + entityName + "&action=insert&formid=" + formid + "&workflowid=" + WorkFlowId + "&worktaskid=" + WorkTaskId + "&FlowCaption=" + WorkFlowCaption;    
                            break;
                        case "Page":
                            string url = cdt.Rows[0]["FilePath"].ToString().Replace('\\', '/');
                            url += "?flag=0&history=" + this.GetStringFromQueryString("history", "") + "&wtid=" + WorkTaskId + "&entity=" + cdt.Rows[0]["ChildCaption"].ToString() + "&title=" + title + "&wfid=" + WorkFlowId + "&FlowCaption=" + WorkFlowCaption;
                            //Server.Transfer("../" + url);
                            URL = "../" + url;
                            break;
                        case "URL":
                            URL = cdt.Rows[0]["FilePath"].ToString();
                            break;
                        default:
                            break;
                    }

                }

            }
        }

     

	}
}